package com.topnav.mc.zzd;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.TimeZone;

/**
 * @Author: kenny
 * @Date: 2024/1/8 11:24
 */
@Slf4j
public class HmacSha1CryptUitl {

    /**
     * MD5加密
     */
    public static String md5s(String plainText) {
        String str = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            str = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


    private static String toHexString(byte[] bytes) {
        Formatter formatter = new Formatter();
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }

    public static String hmacSha1(String value, String key) {
        try {
            byte[] keyBytes = key.getBytes();
            SecretKeySpec signingKey = new SecretKeySpec(keyBytes,"HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(value.getBytes());
            return toHexString(rawHmac);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * SHA1 加密
     * @param input
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String sha1Encrypt(String input) {
        try{
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
            byte[] result = messageDigest.digest(input.getBytes());
            StringBuilder stringBuilder = new StringBuilder();

            for (byte b : result) {
                stringBuilder.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
            }

            return stringBuilder.toString();
        }catch (NoSuchAlgorithmException e){
            log.error(e.toString());
        }catch (Exception e){
            log.error(e.toString());
        }
        return "";
    }

    /**
     * hash 2 hex
     * @param hash
     * @return
     */
    public static String hashToHex(String  hash){

        StringBuilder hexString = new StringBuilder();
        for (byte b : hash.getBytes()) {
            hexString.append(String.format("%02x", b));
        }

        String hexHash = hexString.toString();
        return hexHash;
    }


    /**
     * 格林威治时间和本地时间之间的时差，单位分钟
     * @return
     */
    public static int getTimeOffset(){
        return TimeZone.getDefault().getRawOffset()/1000/60 *-1;
    }


/*    public class ZoneDateTimeTest {
        public void transTimeZoneOffset () {
            int offset = TimeZone.getTimeZone("America/Los_Angeles").getRawOffset();
            // 输出 -8
            System.out.println(offset/(60*60*1000));
        }
    }*/


}
